/*
 * eleConfig.js
 * 要素配置
 */
;
(function (call) {
    var main = {
        init: function () {
            this.initDatePicker();
            this.bindEvt();
            this.initProduct();
        },
        bindEvt: function () {
            var that = this;
            //绘制
            $(".draw").unbind("click").bind("click", function () {
                that.initProduct();
            })
            //下载
            $(".download").unbind("click").bind("click", function () {
                Html2Canvas.Save($("#elePic_contentMain"), function () { });
            })
        },
        // 初始化时间控件
        initDatePicker: function () {
            var that = this;
            // var time = $("#modal_point_pro").data('time');
            // var date = new Date(time + ":00");
            var time = new Date().format("yyyy-MM-dd hh:");
            var firstTime = new Date(new Date().getTime()-1*24*60*60*1000).format("yyyy-MM-dd hh:00");
            $("#elePic-startdatepicker").val(firstTime);
            $("#elePic-enddatepicker").val(time);
            $.IDate(".elePic-query_time",{
                
            })

            /**that.startDatepicker = CafeJS.Datepicker({
                curDate: new Date(new Date().getTime()-1*24*60*60*1000).format("yyyy-MM-dd hh:00"),
                appendTo: "#elePic-startdatepicker",
                dateFmt: "yyyy-MM-dd",
                type: 5,
                timeInterval: 12,
                dateType: 'H',
                customHm: [00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23],
                call: function () {

                },
            });
            that.endDatepicker = CafeJS.Datepicker({
                curDate: time,
                appendTo: "#elePic-enddatepicker",
                dateFmt: "yyyy-MM-dd",
                type: 5,
                timeInterval: 12,
                dateType: 'H',
                customHm: [00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23],
                call: function () {

                },
            });*/
        },
        getReqParam:function(){
            var that = this;
            return {
                elementId:$("#elementId").val(),
                range:$("#areaCode").val(),
                startTime:$("#elePic-startdatepicker").IDatePicker().getVal()+":00",
                endTime:$("#elePic-enddatepicker").IDatePicker().getVal()+":00"
            }
        },
        // 获取产品图
        initProduct: function () {
            var that = this;
            params = that.getReqParam();
            var startTime = new Date(params.startTime).getTime(),endTime = new Date(params.endTime).getTime();
            if(startTime >= endTime){
                alert("结束时间需大于开始时间");
                return;
            }

            $(".title").text(($("#areaCode option:selected").text()+"流域降水").replace("天津流域","天津区域"));
            var subtitle = new Date(startTime).format("MM月dd日hh时mm分")+"-"+new Date(endTime).format("MM月dd日hh时mm分")
            $(".subtitle").text(subtitle);

            //var legendPic = that.getLegend((endTime - startTime)/(1000*60*60));
            var legendPic = "color_tp24h.png";
            let picName = params.range == "0" ? "AFTJ" : "AEHH";
            loading.add();
            $.getJSON("/zhfx/station/list/tpImgByTimeRange", params, function (res) {
                loading.remove();
                if (res.isSuccess) {
                    var data = res.data;
                    if (data) {
                        data = "data:image/png;base64,"+data;
                        $(".model-pic").removeClass('hidden').attr('src', data).show();
                        $(".model-pic-bj").removeClass('hidden').attr('src', '../business/common/css/images/elepic/' + picName + '.png');
                        $(".legend-pic").attr("src", '../business/common/css/images/elepic/' + legendPic + '').show();
                    } else {
                        alert('无产品图');
                        $(".model-pic").removeClass('hidden').attr('src', '').hide();
                        $(".model-pic-bj").removeClass('hidden').attr('src', '../business/common/css/images/elepic/' + picName + '.png');
                        $(".legend-pic").attr("src", '').hide();
                    }
                }
            })
        },
        getLegend:function(hour){
            debugger
            var legend = "color_tp1h.png";
            if(hour >= 24){
                legend = "color_tp24h.png";
            }else if(hour >= 12){
                legend = "color_tp12h.png";
            }else if(hour >= 6){
                legend = "color_tp6h.png";
            }else if(hour >= 3){
                legend = "color_tp3h.png";
            }else{
                legend = "color_tp1h.png";
            }
            return legend;
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    CafeJS.models(['ajax'],function(){
         main.init();
    })
});